/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once
#include "ui_SignOutWidget.h"

class AbstractInventory;
class AbstractHistory;
class AbstractVerifier;
class InventoryModel;
class HistoryModel;

/**
 * A widget that allows the user to sign out items.
 */
class SignOutWidget : public QWidget
{
	Q_OBJECT

public:
	/**
	 * Creates a new sign out widget.
	 * 
	 * @param inventory The inventory containing the list of items that can be signed out.
	 * @param history The history database that records signed out items.
	 * @param verifier Used to verify that the user input is valid.
	 */
	SignOutWidget(AbstractInventory &inventory, AbstractHistory &history, AbstractVerifier &verifier);

	/**
	 * Disposes of any resources used by this object.
	 */
	~SignOutWidget();

private slots:
	/**
	 * Signs out an item.
	 */
	void signOutItem();

private:
	/**
	 * Verifies that the fields (username, password, item) are valid.
	 * This only includes checks for emptiness, not full validation, which is provided
	 * by an AbstractVerifier.
	 */
	bool verifyFields() const;

	/**
	 * Shows an error message and focuses on a widget if it's input is not valid.
	 *
	 * @param widget The widget to validate.
	 * @param isValid Whether or not the widget's input is valid.
	 * @param errorMessage The message to display if isValid is false.
	 */
	bool verifyField(QWidget *widget, bool isValid, const QString errorMessage) const;

	Ui::SignOutWidget ui;

	AbstractInventory *inventory;
	AbstractHistory *history;
	AbstractVerifier *verifier;
	InventoryModel *inventoryModel;
	HistoryModel *historyModel;
};

